#!/usr/bin/python

import os, sys, time
from SOAPpy import SOAPProxy

from ivic.vspace.VSpace import *
from ivic.vspace.Server import *

default_user = "vsaas"

def usage():
    print "ivic-vspace [view|create [user]]. Default user is vsaas."

class VSpaceServerProxy(object):
    
    def __init__(self, server=None):
        if server == None: self.server = "127.0.0.1:8080"
        self.proxy = SOAPProxy('http://%s/vspace' % self.server)
        
    def create(self, user):
        try:
            op = getattr(self.proxy, 'createVSpace')
            #args = {'user':user, 'software':software, 'display':display}
            retval = op(user)
            print repr(retval)
        except Exception, e:
            print e
        
    def kill(self, vnc_address):
        try:
            op = getattr(self.proxy, 'deleteVSpace')
            retval = op(vnc_address)
        except Exception, e:
            print e
        
    def queryAll(self):
        op = getattr(self.proxy, 'queryAllVSpaceString')
        retval = op()
        for each in retval: print each
    
def proxymain(argv):
    try:
        proxy = VSpaceServerProxy()
    except:
        print "server not started"
        return
    
    if len(argv) <= 1: 
        usage()
        return
    else:
        argv = argv[1:]
        
    if argv[0] == 'create':
        try:
            if(len(argv) > 1):
                user = argv[1]
            else:
                user = default_user;
                
            proxy.create(user)
        except Exception, e:
            print e
            
    elif argv[0] == 'view':
        try:
            proxy.queryAll()
        except:
            pass
    elif argv[0] == 'kill':
        try:
            proxy.kill(argv[1])
        except Exception, e:
            print e
    else:
        #to be done with other options
        pass

